package cn.icanci.rec.admin.dal.utils.service.impl;

import cn.icanci.rec.admin.dal.utils.IDHolder;
import cn.icanci.rec.admin.dal.utils.service.IDGeneratorService;
import cn.icanci.rec.admin.dal.utils.service.SnowFlake;

import org.apache.commons.lang3.RandomUtils;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Service;

/**
 * 分布式ID生成器
 * 
 * @author icanci
 * @since 1.0 Created in 2022/10/30 08:43
 */
@Service
public class IDGeneratorServiceImpl implements IDGeneratorService, InitializingBean {
    /** 雪花序列号生成算法 */
    private final static SnowFlake SNOW_FLAKE = new SnowFlake(RandomUtils.nextInt(1, 9), RandomUtils.nextInt(1, 9));

    @Override
    public String generateNoBySnowFlake(String prefix) {
        return prefix + SNOW_FLAKE.nextId();
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        IDHolder.setIdGeneratorService(this);
    }
}
